﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    [Description("1调拨申请单批量更改金额字段")]
    [HotUpdate]
    public class TransferApplyCilck : AbstractListPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            //选择的行,获取所有信息,放在listcoll里面
            if (e.BarItemKey.Equals("tbB_BOS_UpdateAmount", StringComparison.OrdinalIgnoreCase))
            {
                ListSelectedRowCollection listcoll = this.ListView.SelectedRowsInfo;

                DynamicObjectCollection dycoll = this.ListModel.GetData(listcoll);
                for (int i = 0; i < dycoll.Count; i++)
                {
                    string fbillno = dycoll[i]["FBillNo"].ToString();
                    string MATERIALID = dycoll[i]["FMATERIALID_Id"].ToString();
                    string FSTOCKORGID = dycoll[i]["FStockOrgId_Id"].ToString();
                    string FENTRYID1 = dycoll[i]["t1_FENTRYID"].ToString();
                    double FBASEQTY = double.Parse(dycoll[i]["FBASEQTY"].ToString());
                    double FQTY = double.Parse(dycoll[i]["FQTY"].ToString());

                    string sql = string.Format(@"select a.FNUMBER,b.FERPCLSID from T_BD_MATERIAL a
                                                inner join T_BD_MATERIALBASE b on  a.FMATERIALID = b.FMATERIALID
                                                where a.FMATERIALID='" + MATERIALID + "'");
                    DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                    if (sql_toStirng.Rows.Count > 0)
                    {
                        //物料属性
                        string FERPCLSID = sql_toStirng.Rows[0]["FERPCLSID"].ToString();
                        //物料编码
                        string fnumber = sql_toStirng.Rows[0]["FNUMBER"].ToString();

                        //物料属性为自制
                        if (FERPCLSID.Equals("2") || FERPCLSID.Equals("3"))
                        {
                            //查询物料在组织间结算价目标中单据状态，行状态，是否否审核，是否在生效日和失效日期间
                            string SQL = string.Format(@"/*dialect*/ SELECT A.FBILLNO,B.FENTRYID,B.FMATERIALID,e.FPRICE,E.FTAXPRICE  FROM T_STK_STKTRANSFERAPP A
                                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY B ON A.FID=B.FID
                                                    INNER JOIN T_BD_MATERIAL C ON B.FMATERIALID=C.FMATERIALID
                                                    INNER JOIN T_BD_MATERIAL CD ON C.FNUMBER = CD.FNUMBER AND B.FSTOCKORGID=CD.FUSEORGID
                                                    INNER JOIN T_BD_MATERIALBASE D on CD.FMATERIALID = D.FMATERIALID   
                                                    INNER JOIN T_IOS_PRICELISTENTRY E ON E.FMATERIALID=  CD.FMATERIALID AND  CONVERT(DATE, GETDATE()) BETWEEN E.FEFFECTIVEDATE AND E.FEXPRIYDATE AND E.FROWAUDITSTATUS='A' 
                                                    INNER JOIN T_IOS_PRICELIST F ON E.FID=F.FID AND CONVERT(DATE, GETDATE()) BETWEEN F.FEFFECTIVEDATE AND F.FEXPIRYDATE AND F.FDOCUMENTSTATUS='C'
                                                    WHERE FTRANSTYPE='OverOrgTransfer' AND F_QLTK_Combo_tzk='2' and A.FBILLNO='" + fbillno + "' and  b.FMATERIALID='"+ MATERIALID + "'order by F.FCREATEDATE desc");
                            DataTable dt = DBUtils.ExecuteDataSet(this.Context, SQL).Tables[0];
                            if (dt.Rows.Count > 0)
                            {
                                for (int j = 0; j < dt.Rows.Count; j++)
                                {
                                    string FENTRYID = dt.Rows[j]["FENTRYID"].ToString();
                                    double FPRICE = double.Parse(dt.Rows[j]["FPRICE"].ToString());//含税单价

                                    double FTaxPrice = double.Parse(dt.Rows[j]["FTAXPRICE"].ToString());//含税单价

                                    double pirce = FBASEQTY * FPRICE / FQTY;

                                    string SQL1 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_RDLV_HSDJ='" + pirce + "' WHERE FENTRYID='" + FENTRYID + "'");
                                    DBUtils.Execute(this.Context, SQL1);
                                    string SQL3 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_QLTK_TAXPRICE=(F_RDLV_HSDJ*(1+F_QLTK_SL/100.0)) WHERE FENTRYID='" + FENTRYID + "'");
                                    DBUtils.Execute(this.Context, SQL3);
                                    string SQL2 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_QLTK_Amount_qtr=F_RDLV_HSDJ*FBASEQTY,F_QLTK_AllAmount=F_QLTK_TaxPrice*FBASEQTY  WHERE FENTRYID='" + FENTRYID + "'");
                                    DBUtils.Execute(this.Context, SQL2);
                                    this.View.ShowMessage("材料调拨重取价格成功");
                                }
                            }
                        }
                        else if (FERPCLSID.Equals("1"))
                        {
                            DateTime now = DateTime.Now;
                            int currentYear = now.Year;
                            //取调出组织的最后期初结存价格
                            string sql2 = string.Format(@"SELECT   
                                                    C.FFINORGID AS FORGID,  
                                                    M.FMASTERID,  
                                                    M.FNUMBER,  
                                                    O.FYEAR,  
                                                    O.FPERIOD,  
                                                    SUM(CASE  WHEN HB.FENDINITKEY = 0 THEN HB.FQTY ELSE 0 END) 期初数, 
                                                    SUM(CASE  WHEN HB.FENDINITKEY = 0 THEN HB.FAMOUNT ELSE 0 END) 期初金额,
                                                    SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FQTY ELSE 0 END) AS 期末数,
                                                    SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FAMOUNT ELSE 0 END) AS 期末金额,
                                                    SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FOUTSTOCKQTY ELSE 0 END) AS 出库数,
                                                    SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FCURRENTOUTAMOUNT ELSE 0 END) AS 出库金额
                                                     FROM   
                                                         T_HS_BALANCE HB  
                                                     LEFT JOIN T_HS_STOCKDIMENSION S ON HB.FDIMEENTRYID=S.FENTRYID  
                                                     LEFT JOIN T_HS_CALDIMENSIONS C ON HB.FDIMENSIONID=C.FDIMENSIONID  
                                                     LEFT JOIN T_HS_OUTACCTG O ON HB.FID=O.FID  
                                                     LEFT JOIN T_BD_MATERIAL M ON S.FMASTERID=M.FMASTERID AND C.FFINORGID=M.FUSEORGID  
                                                     LEFT JOIN T_ORG_ORGANIZATIONS_L L ON L.FORGID=C.FFINORGID AND L.FLOCALEID=2052  
                                                     LEFT JOIN T_BD_STOCK AS ST ON ST.FSTOCKID =S.FSTOCKID  
                                                     WHERE O.FYEAR='{0}' and m.FNUMBER='{1}' and C.FFINORGID='{2}'
                                                     GROUP BY C.FFINORGID, M.FMASTERID, O.FYEAR, O.FPERIOD, M.FNUMBER  ", currentYear, fnumber, FSTOCKORGID);
                            DataTable sql_toStirng2 = DBUtils.ExecuteDataSet(this.Context, sql2).Tables[0];
                            if (sql_toStirng2.Rows.Count > 0)
                            {
                                double FTaxPrice = 0;
                                if (double.Parse(sql_toStirng2.Rows[0]["期末数"].ToString()) == 0)
                                {
                                    if (double.Parse(sql_toStirng2.Rows[0]["出库数"].ToString()) == 0)
                                    {
                                        FTaxPrice = 0;//期初金额
                                    }
                                    else
                                    {
                                        FTaxPrice = double.Parse(sql_toStirng2.Rows[0]["出库金额"].ToString()) / double.Parse(sql_toStirng2.Rows[0]["出库数"].ToString());
                                        FTaxPrice = FBASEQTY * FTaxPrice / FQTY;
                                    }
                                }
                                else
                                {
                                    FTaxPrice = double.Parse(sql_toStirng2.Rows[0]["期末金额"].ToString()) / double.Parse(sql_toStirng2.Rows[0]["期末数"].ToString());
                                    FTaxPrice = FBASEQTY * FTaxPrice / FQTY;

                                }

                                string SQL1 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_RDLV_HSDJ='" + FTaxPrice + "' WHERE FENTRYID='" + FENTRYID1 + "'");
                                DBUtils.Execute(this.Context, SQL1);
                                string SQL2 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_QLTK_TaxPrice = F_RDLV_HSDJ*(1+F_QLTK_SL/100.0)  WHERE FENTRYID='" + FENTRYID1 + "'");
                                DBUtils.Execute(this.Context, SQL2);
                                string SQL3 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_QLTK_Amount_qtr=F_RDLV_HSDJ*FBASEQTY,F_QLTK_AllAmount=F_QLTK_TaxPrice*FBASEQTY  WHERE FENTRYID='" + FENTRYID1 + "'");
                                DBUtils.Execute(this.Context, SQL3);
                                this.View.ShowMessage("材料调拨重取价格成功");

                            }
                        }
                        string sql3 = string.Format(@"/*dialect*/ UPDATE B SET B.F_QLTK_HSPRICE = E.F_QLTK_TAXPRICE,B.F_QLTK_HSAmount=E.F_QLTK_ALLAMOUNT
                                                    FROM T_STK_STKTRANSFEROUT  A --分布式调出单
                                                    INNER JOIN T_STK_STKTRANSFEROUTENTRY B ON A.FID = B.FID --分布式调出单明细
                                                    INNER JOIN T_STK_STKTRANSFEROUTENTRY_LK C ON B.FENTRYID=C.FENTRYID AND FRULEID='STK_TRANSFERAPPLY-STK_TRANSFEROUT'--分布式调出单明细关联表
                                                    INNER JOIN T_STK_STKTRANSFERAPP D ON C.FSBILLID = D.FID AND D.F_QLTK_Combo_tzk='2' AND D.FTRANSTYPE='OverOrgTransfer' --调拨申请单
                                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY E ON D.FID=E.FID AND C.FSID = E.FENTRYID  --调拨申请单明细
                                                    INNER JOIN T_BD_MATERIALBASE F ON E.FMATERIALID = F.FMATERIALID 
                                                    where d.FBILLNO='" + fbillno+"'");
                        DBUtils.Execute(this.Context, sql3);
                        string sql4 = string.Format(@"/*dialect*/ UPDATE RINE SET RINE.F_QLTK_HSPRICE = b.F_QLTK_HSPRICE,RINE.F_QLTK_HSAmount=b.F_QLTK_HSAmount
                                            FROM T_STK_STKTRANSFERIN RIN --分布式调入单据
                                            INNER JOIN T_STK_STKTRANSFERINENTRY RINE ON RIN.FID = RINE.FID --分布式调入单据明细
                                            INNER JOIN T_STK_STKTRANSFERINENTRY_LK RINEL ON RINE.FENTRYID = RINEL.FENTRYID AND RINEL.FRULEID='STK_TRANSFEROUT-STK_TRANSFERIN' --分布式调入单明细关联表
                                            INNER JOIN T_STK_STKTRANSFEROUT  A ON RINEL.FSBILLID = A.FID--分布式调出单
                                            INNER JOIN T_STK_STKTRANSFEROUTENTRY B ON A.FID = B.FID AND RINEL.FSID = B.FENTRYID--分布式调出单明细
                                            INNER JOIN T_STK_STKTRANSFEROUTENTRY_LK C ON B.FENTRYID=C.FENTRYID AND C.FRULEID='STK_TRANSFERAPPLY-STK_TRANSFEROUT'--分布式调出单明细关联表
                                            INNER JOIN T_STK_STKTRANSFERAPP D ON C.FSBILLID = D.FID AND D.F_QLTK_Combo_tzk='2' AND D.FTRANSTYPE='OverOrgTransfer' --调拨申请单
                                            INNER JOIN T_STK_STKTRANSFERAPPENTRY E ON D.FID=E.FID AND C.FSID = E.FENTRYID  --调拨申请单明细
                                            INNER JOIN T_BD_MATERIALBASE F ON E.FMATERIALID = F.FMATERIALID --物料基本表
                                            where D.FBILLNO='" + fbillno+"' ");
                        DBUtils.Execute(this.Context, sql4);
                    }
                }
            }

        }
    }
}
